﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using XF.IDAL;
using XF.DBUtility;//Please add references
using System.Collections;
using XF.Common;

namespace XF.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:Bill_STOHead
	/// </summary>
	public partial class Bill_STOHead:IBill_STOHead
	{
		public Bill_STOHead()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return SqlServerHelper.GetMaxID("Id", "Bill_STOHead"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Bill_STOHead");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return SqlServerHelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(XF.Model.Bill_STOHead model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Bill_STOHead(");
			strSql.Append("OrderCode,OrderDate,CustomerId,Remark,CreateDate,CreateUser,LastUpdateDate,LastUpdateUser,Enable)");
			strSql.Append(" values (");
			strSql.Append("@OrderCode,@OrderDate,@CustomerId,@Remark,@CreateDate,@CreateUser,@LastUpdateDate,@LastUpdateUser,@Enable)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,64),
					new SqlParameter("@OrderDate", SqlDbType.DateTime),
					new SqlParameter("@CustomerId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@CreateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@Enable", SqlDbType.Bit,1)};
			parameters[0].Value = model.OrderCode;
			parameters[1].Value = model.OrderDate;
			parameters[2].Value = model.CustomerId;
			parameters[3].Value = model.Remark;
			parameters[4].Value = model.CreateDate;
			parameters[5].Value = model.CreateUser;
			parameters[6].Value = model.LastUpdateDate;
			parameters[7].Value = model.LastUpdateUser;
			parameters[8].Value = model.Enable;

			object obj = SqlServerHelper.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(XF.Model.Bill_STOHead model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Bill_STOHead set ");
			strSql.Append("OrderCode=@OrderCode,");
			strSql.Append("OrderDate=@OrderDate,");
			strSql.Append("CustomerId=@CustomerId,");
			strSql.Append("Remark=@Remark,");
			strSql.Append("LastUpdateDate=@LastUpdateDate,");
			strSql.Append("LastUpdateUser=@LastUpdateUser,");
			strSql.Append("Enable=@Enable");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,64),
					new SqlParameter("@OrderDate", SqlDbType.DateTime),
					new SqlParameter("@CustomerId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@Enable", SqlDbType.Bit,1),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.OrderCode;
			parameters[1].Value = model.OrderDate;
			parameters[2].Value = model.CustomerId;
			parameters[3].Value = model.Remark;
			parameters[4].Value = model.LastUpdateDate;
			parameters[5].Value = model.LastUpdateUser;
			parameters[6].Value = model.Enable;
			parameters[7].Value = model.Id;

			int rows= SqlServerHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Bill_STOHead ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			int rows= SqlServerHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Bill_STOHead ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows= SqlServerHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.Bill_STOHead GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Id,OrderCode,OrderDate,CustomerId,CreateDate,CreateUser,LastUpdateDate,LastUpdateUser,Enable,Remark from Bill_STOHead ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			XF.Model.Bill_STOHead model=new XF.Model.Bill_STOHead();
			DataSet ds= SqlServerHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.Bill_STOHead DataRowToModel(DataRow row)
		{
			XF.Model.Bill_STOHead model=new XF.Model.Bill_STOHead();
			if (row != null)
			{
				if(row["Id"]!=null && row["Id"].ToString()!="")
				{
					model.Id=int.Parse(row["Id"].ToString());
				}
				if(row["OrderCode"]!=null)
				{
					model.OrderCode=row["OrderCode"].ToString();
				}
				if(row["OrderDate"]!=null && row["OrderDate"].ToString()!="")
				{
					model.OrderDate=DateTime.Parse(row["OrderDate"].ToString());
				}
				if(row["CustomerId"]!=null && row["CustomerId"].ToString()!="")
				{
					model.CustomerId=int.Parse(row["CustomerId"].ToString());
				}
				if (row["Remark"] != null)
				{
					model.Remark = row["Remark"].ToString();
				}
				if (row["CreateDate"]!=null && row["CreateDate"].ToString()!="")
				{
					model.CreateDate=DateTime.Parse(row["CreateDate"].ToString());
				}
				if(row["CreateUser"]!=null)
				{
					model.CreateUser=row["CreateUser"].ToString();
				}
				if(row["LastUpdateDate"]!=null && row["LastUpdateDate"].ToString()!="")
				{
					model.LastUpdateDate=DateTime.Parse(row["LastUpdateDate"].ToString());
				}
				if(row["LastUpdateUser"]!=null)
				{
					model.LastUpdateUser=row["LastUpdateUser"].ToString();
				}
				if(row["Enable"]!=null && row["Enable"].ToString()!="")
				{
					if((row["Enable"].ToString()=="1")||(row["Enable"].ToString().ToLower()=="true"))
					{
						model.Enable=true;
					}
					else
					{
						model.Enable=false;
					}
				}
				#region 扩展属性
				if(row.Table.Columns.Contains("CustomerCode"))
				{
					if (row["CustomerCode"] != null)
					{
						model.CustomerCode = row["CustomerCode"].ToString();
					}
				}
				if (row.Table.Columns.Contains("CustomerName"))
				{
					if (row["CustomerName"] != null)
					{
						model.CustomerName = row["CustomerName"].ToString();
					}
				}
				if (row.Table.Columns.Contains("PayType"))
				{
					if (row["PayType"] != null && row["PayType"].ToString() != "")
					{
						model.PayType = int.Parse(row["PayType"].ToString());
					}
				}
				if (row.Table.Columns.Contains("CustomerRemark"))
				{
					if (row["CustomerRemark"] != null)
					{
						model.CustomerRemark = row["CustomerRemark"].ToString();
					}
				}
				if (row.Table.Columns.Contains("Contacts"))
				{
					if (row["Contacts"] != null)
					{
						model.CustomerContacts = row["Contacts"].ToString();
					}
				}
				if (row.Table.Columns.Contains("Phone"))
				{
					if (row["Phone"] != null)
					{
						model.CustomerPhone = row["Phone"].ToString();
					}
				}

				if (row.Table.Columns.Contains("No"))
				{
					if (row["No"] != null && row["No"].ToString() != "")
					{
						model.CustomerNo = int.Parse(row["No"].ToString());
					}
				}
				if (row.Table.Columns.Contains("Account"))
				{
					if (row["Account"] != null)
					{
						model.CustomerAccount = row["Account"].ToString();
					}
				}
				if (row.Table.Columns.Contains("Address"))
				{
					if (row["Address"] != null)
					{
						model.CustomerAddress = row["Address"].ToString();
					}
				}
				#endregion
			}
            return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,OrderCode,OrderDate,CustomerId,CreateDate,CreateUser,LastUpdateDate,LastUpdateUser,Enable ");
			strSql.Append(" FROM Bill_STOHead ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SqlServerHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Id,OrderCode,OrderDate,CustomerId,CreateDate,CreateUser,LastUpdateDate,LastUpdateUser,Enable ");
			strSql.Append(" FROM Bill_STOHead ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return SqlServerHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Bill_STOHead ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = SqlServerHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from Bill_STOHead T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return SqlServerHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Bill_STOHead";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod

		#region  ExtensionMethod
		// <summary>
		/// 获取分页数据
		/// </summary>
		/// <param name="strWhere"></param>
		/// <param name="strOrder"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <param name="count"></param>
		/// <returns></returns>
		public DataTable GetListByPage(string strWhere, string strOrder, int pageIndex, int pageSize, ref int count)
		{
			string tbName = " Bill_STOHead A ";
			string tbGetFields = " A.* ";
			return SqlServerHelper.GetPageList(tbName, tbGetFields, strOrder, pageSize, pageIndex, strWhere, ref count);
		}

		public DataTable GetDetailListByPage(string strWhere, string strOrder, int pageIndex, int pageSize, ref int count)
		{
			string tbName = " V_Bill_STOHead A ";
			string tbGetFields = " A.* ";
			return SqlServerHelper.GetPageList(tbName, tbGetFields, strOrder, pageSize, pageIndex, strWhere, ref count);
		}

		public DictionaryEntry GetAddSql(XF.Model.Bill_STOHead model)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into Bill_STOHead(");
			strSql.Append("OrderCode,OrderDate,CustomerId,Remark,CreateDate,CreateUser,LastUpdateDate,LastUpdateUser,Enable)");
			strSql.Append(" values (");
			strSql.Append("@OrderCode,@OrderDate,@CustomerId,@Remark,@CreateDate,@CreateUser,@LastUpdateDate,@LastUpdateUser,@Enable);");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,64),
					new SqlParameter("@OrderDate", SqlDbType.DateTime),
					new SqlParameter("@CustomerId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@CreateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@Enable", SqlDbType.Bit,1)};
			parameters[0].Value = model.OrderCode;
			parameters[1].Value = model.OrderDate;
			parameters[2].Value = model.CustomerId;
			parameters[3].Value = model.Remark;
			parameters[4].Value = model.CreateDate;
			parameters[5].Value = model.CreateUser;
			parameters[6].Value = model.LastUpdateDate;
			parameters[7].Value = model.LastUpdateUser;
			parameters[8].Value = model.Enable;
			return new DictionaryEntry(strSql.ToString(), parameters);
		}

		public DictionaryEntry GetUpdateSql(XF.Model.Bill_STOHead model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Bill_STOHead set ");
			strSql.Append("OrderCode=@OrderCode,");
			strSql.Append("OrderDate=@OrderDate,");
			strSql.Append("CustomerId=@CustomerId,");
			strSql.Append("Remark=@Remark,");
			strSql.Append("LastUpdateDate=@LastUpdateDate,");
			strSql.Append("LastUpdateUser=@LastUpdateUser,");
			strSql.Append("Enable=@Enable");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,64),
					new SqlParameter("@OrderDate", SqlDbType.DateTime),
					new SqlParameter("@CustomerId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@Enable", SqlDbType.Bit,1),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.OrderCode;
			parameters[1].Value = model.OrderDate;
			parameters[2].Value = model.CustomerId;
			parameters[3].Value = model.Remark;
			parameters[4].Value = model.LastUpdateDate;
			parameters[5].Value = model.LastUpdateUser;
			parameters[6].Value = model.Enable;
			parameters[7].Value = model.Id;
			return new DictionaryEntry(strSql.ToString(), parameters);
		}

		/// <summary>
		/// 批量取消
		/// </summary>
		public bool DisableList(string Idlist)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Bill_STOHead set Enable = 0");
			strSql.Append(" where Id in (" + Idlist + ")  ");
			int rows = SqlServerHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 批量恢复
		/// </summary>
		public bool EnableList(string Idlist)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Bill_STOHead set Enable = 1");
			strSql.Append(" where Id in (" + Idlist + ")  ");
			int rows = SqlServerHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetDetailList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM V_Bill_STOHead ");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}
			return SqlServerHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.Bill_STOHead GetDetailModel(int Id)
		{
			DataSet ds = GetDetailList(" Id = " + Id);
			if (ds.Tables[0].Rows.Count > 0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获取指定日期、用户的最大流水号
		/// </summary>
		/// <param name="userId"></param>
		/// <param name="dateTime"></param>
		/// <returns></returns>
		public int GetMaxOrderCode(string mrp, DateTime dateTime)
		{
			string prefix = dateTime.ToString("yyyyMMdd") + "%" + mrp;
			string sql = "select ISNULL(MAX(OrderCode),'') from Bill_STOHead where OrderCode like '" + prefix + "' and LEN(OrderCode) = 12";
			DataTable dt = SqlServerHelper.Query(sql).Tables[0];
			string orderCode = dt.Rows[0][0].ToString();
			if (orderCode.Equals(string.Empty))
			{
				return 0;
			}
			else
			{
				return zDataConverter.ToInt(orderCode.Substring(orderCode.Length - 4, 3));
			}
		}
		#endregion  ExtensionMethod
	}
}

